package com.unohacha.qqj.fzdr.personal.view.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.common.bean.HttpResult;
import com.unohacha.qqj.fzdr.common.bean.Meterialdetail2Bean;
import com.unohacha.qqj.fzdr.common.http.RetrofitManager;
import com.unohacha.qqj.fzdr.util.ToastUtils;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class MaterialDetail2Activity extends BaseActivity implements OnRefreshListener, OnLoadmoreListener {
    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.mRefreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private String address_id;//地区id
    private String id;//分类id
    private String name;//分类id
    private int page = 1;
    private MyAdapter adapter;
    private List<Meterialdetail2Bean.DataBean> list;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_material_2;
    }

    @Override
    protected void initView() {
        commonRightImage.setVisibility(View.INVISIBLE);
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setOnLoadmoreListener(this);
        if (null != this.getIntent().getStringExtra("name")) {
            commonTitle.setText(this.getIntent().getStringExtra("name"));
            name = this.getIntent().getStringExtra("name");
        }
        address_id = this.getIntent().getStringExtra("address_id");
        id = this.getIntent().getStringExtra("id");
        list = new ArrayList<>();
        recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new MyAdapter(R.layout.item_meterialdetail2, list);
        recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(MaterialDetail2Activity.this, MaterialDetail3Activity.class);
                intent.putExtra("address_id", address_id + "");
                intent.putExtra("member_id", list.get(position).getMember_id() + "");
                startActivity(intent);
            }
        });
        getMessage();
    }

    @OnClick(R.id.common_back)
    public void onViewClicked() {
        finish();
    }

    private void closeRefresh() {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.finishRefresh();
        }
        if (mRefreshLayout.isLoading()) {
            mRefreshLayout.finishLoadmore();
        }
    }

    private void getMessage() {
        showLoading();
        RetrofitManager.getInstance()
                .MDetail2(id, address_id, page + "")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Action1<HttpResult<Meterialdetail2Bean>>() {
                               @Override
                               public void call(HttpResult<Meterialdetail2Bean> meterialdetail2BeanHttpResult) {
                                   if (meterialdetail2BeanHttpResult.getCode() == 1) {
                                       if (null != meterialdetail2BeanHttpResult.getData().getData()) {
                                           list.addAll(meterialdetail2BeanHttpResult.getData().getData());
                                           adapter.notifyDataSetChanged();
                                       }

                                   } else if (meterialdetail2BeanHttpResult.getCode() == 0) {
                                       mRefreshLayout.finishLoadmoreWithNoMoreData();
                                       ToastUtils.showT(meterialdetail2BeanHttpResult.getMessage());
                                   }
                                   closeRefresh();
                               }
                           }, new Action1<Throwable>() {
                               @Override
                               public void call(Throwable throwable) {
                                   closeRefresh();
                               }
                           }
                );
        hideLoading();
    }

    private class MyAdapter extends BaseQuickAdapter<Meterialdetail2Bean.DataBean, BaseViewHolder> {
        public MyAdapter(int layoutResId, @Nullable List<Meterialdetail2Bean.DataBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Meterialdetail2Bean.DataBean item) {
            helper.setText(R.id.tv_name, item.getMember_no() + "");
            helper.setText(R.id.tv_time, item.getTime() + "");
            helper.setText(R.id.tv_money, "￥"+item.getPrice() + "");
        }
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        page++;
        getMessage();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        page = 1;
        list.clear();
        getMessage();
    }
}
